﻿using System;
using System.Collections.Generic;
using System.Text;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Cards;
using LBoL.Core.StatusEffects;
using LBoL.Core.Units;
using LBoL.EntityLib.Cards.Neutral.Blue;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    public sealed class ProbeProbeBeingAttackedDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;

            config.Colors = new List<ManaColor>() { ManaColor.White };
            config.Cost = new ManaGroup() { White = 1 ,Any=1 };
            config.UpgradedCost = new ManaGroup() { White = 1,Any = 1 };
            config.AutoPerform = false;

            config.Value1 = 1;
            config.Value2 = 1;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.RelativeCards = new List<string>() { "ProbeProbeBeingAttackedDeployed" , "ProbeZealotCharge" };
            config.UpgradedRelativeCards = new List<string>() { "ProbeProbeBeingAttackedDeployed", "ProbeZealotCharge" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 你的探机遭到攻击！
    /// 部署
    ///     若探机受到伤害，将3张狂热者冲锋置入弃牌堆，抽2张牌/将2张狂热者冲锋置入手牌
    /// </summary>
    [EntityLogic(typeof(ProbeProbeBeingAttackedDef))]
    public sealed class ProbeProbeBeingAttacked : ProbeCard
    {
        public override bool RemoveFromBattleAfterPlay => false;

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            //  消耗自身，给一张已部署的结界
            yield return new AddCardsToHandAction(Library.CreateCard<ProbeProbeBeingAttackedDeployed>(IsUpgraded));
            yield return new RemoveCardAction(this);
        }
    }

    public sealed class ProbeProbeBeingAttackedDeployedDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;
            config.IsPooled = false;
            config.Revealable = true;

            config.Colors = new List<ManaColor>() { ManaColor.White };
            config.Cost = ManaGroup.Empty;
            config.UpgradedCost = ManaGroup.Empty;
            config.AutoPerform = true;

            config.Keywords = Keyword.Retain | Keyword.Forbidden;
            config.UpgradedKeywords = Keyword.Retain | Keyword.Forbidden;

            config.Value1 = 1;
            config.Value2 = 1;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 你的探机遭到攻击！已部署
    /// 部署
    ///     若探机遭到攻击，将2张狂热者冲锋置入弃牌堆，抽1张牌/将1张狂热者冲锋置入手牌
    /// </summary>
    [EntityLogic(typeof(ProbeProbeBeingAttackedDeployedDef))]
    public sealed class ProbeProbeBeingAttackedDeployed : ProbeCard
    {
        public override bool RemoveFromBattleAfterPlay => true;

        protected override void OnEnterBattle(BattleController battle)
        {
            ReactBattleEvent(base.Battle.Player.TurnStarted, OnPlayerTurnStarting);
            ReactBattleEvent(base.Battle.Player.DamageTaking, OnPlayerDamageTaking);
        }

        private IEnumerable<BattleAction> OnPlayerDamageTaking(DamageEventArgs args)
        {
            if ( !args.DamageInfo.ZeroDamage && !args.DamageInfo.IsGrazed && args.DamageInfo.DamageType == DamageType.Attack) 
            {
                if (IsUpgraded)
                    yield return new AddCardsToHandAction(Library.CreateCards<ProbeZealotCharge>(Value1));
                else
                    yield return new DrawManyCardAction(Value1);
                yield return new AddCardsToDiscardAction(Library.CreateCards<ProbeZealotCharge>(Value2));
            }
        }

        private IEnumerable<BattleAction> OnPlayerTurnStarting(UnitEventArgs args)
        {
            if (!base.Battle.BattleShouldEnd && base.Zone == CardZone.Hand)
            {
                // 回合开始，消耗自身，转化成目标卡牌
                yield return new AddCardsToDiscardAction(Library.CreateCard<ProbeProbeBeingAttacked>(IsUpgraded));
                yield return new RemoveCardAction(this);
            }
        }

    }
}
